$(function(){
    var results_table = $("#search-results");
    var table_headers = $("tr:eq(0)", results_table);
    // FIXME /media/ should be {{ MEDIA_URL }} somehow
    var ajax_loader = '<img src="/media/img/ajax-loader.gif" />';

    function show_data(data) {
        results_table.show();
        results_table.html(table_headers);
        $('tbody', results_table).append(data);
        activate_quickadd_forms();
        $('.loading').hide();
    };

    var options = {
        success:    show_data
    };


    $("#search-form").submit(function(){
        $('#search-results').hide();
        $('.loading').show();
        $(this).ajaxSubmit(options);
        return false;
    });
});

function activate_quickadd_forms(){
     var quickadd_options = {
        'success': function(data){
            var response = eval('(' + data + ')');
            var course_row = $("#course-" + response['course_id']);
            course_row.addClass('added');
            $('.loading', course_row).css('display', 'none');
            $('form', course_row).css('display', 'none');
            $('.done', course_row).css('display', 'inline');
            $('#term-' + response['course_id'], course_row).html(response['term']);
            // $("#course-" + data).fadeOut('medium');
        },
        'beforeSubmit': function(data, form){
            form.siblings('.add').css('display', 'none');
            form.siblings('.loading').css('display', 'inline');


        }
    }
    $("#QuickAdd select").change(function(){
            $(this).parent().ajaxSubmit(quickadd_options);
    });
};
